package ws.very.util.play2

import play.api.mvc._
import scala.concurrent.Future

trait RichFilterAction[R[_]] extends ActionRefiner[R, R] { self =>
  def filter[A](request: R[A]): Future[Option[Result]]

  protected final def refine[A](request: R[A]): Future[Either[Result, R[A]]] =
    filter(request).map(_.toLeft(request))(executionContext)
  
  def orElse(other: RichFilterAction[R]) = new RichFilterAction[R] {
    def filter[A](request: R[A]): Future[Option[Result]] = {
      self.filter(request).flatMap {
        case Some(result) => other.filter(request)
        case None         => Future.successful(None)
      }(executionContext)
    }
  }
}